package core.server;

import core.util.ErrorList;


class CheckIdleServer extends Thread{
	private ServerHandler<?> clientHandler;
	private long timeWait;
	private boolean isLoop = true;
	public CheckIdleServer(ServerHandler<?> clientHandler, long timeWait){
		this.clientHandler = clientHandler;
		this.timeWait = timeWait;
	}
	
	public void stopCheckIdle(){
		this.isLoop = false;
	}
	
	@Override
	public void run() {
		checkIdle();
		super.run();
	}
	
	private void checkIdle(){
		if (timeWait <= 0) {
			return;
		}
		while (isLoop) {
			try {
				synchronized (this) {
					wait(timeWait);
				}
			} catch (InterruptedException e) {
				clientHandler.onError(e, ErrorList.E_IDLE);
			}
			if (!clientHandler.isWait()) {
				clientHandler.onIdle();
			}
		}
	}
	
}
